package com.itboye.ihomebank.activity.electric_meter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.electric_meter.adapter.AddressListAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.ElectricMeterBean;
import com.itboye.ihomebank.bean.MeterDetailBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.ElectricMeterPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.PtrHTFrameLayout;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2018/8/24 0024.
 */

public class ActivityHouseAddress extends BaseOtherActivity implements Observer {

    View v_statusbar;
    ImageView close_icon;
    TextView add_shap_title_tv;

    ListView electricAddressList;
    List<ElectricMeterBean> meterBeans;
    List<MeterDetailBean> meters;
    AddressListAdapter addressListAdapter;
    ElectricMeterPresenter presenter;
    Intent mIntent;

    PtrHTFrameLayout commend_anchor_ptr;
    String  uid;
    static int RESULTCODE=804;
    @Override
    protected int layout_id() {
        return R.layout.activity_houseaddress;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("房屋地址");

        mIntent=getIntent();
        meterBeans=new ArrayList<>();
        meters = new ArrayList<>();
        uid = SPUtils.get(this,null, SPContants.USER_ID,"")+"";
        presenter = new ElectricMeterPresenter(this);

        presenter.queryMyEmeter(uid,"","");
        BasePtr.setRefreshOnlyStyle(commend_anchor_ptr);
        commend_anchor_ptr.autoRefresh(true);
        commend_anchor_ptr.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //在这里写自己下拉刷新数据的请求
                //需要结束刷新头
                uid = SPUtils.get(ActivityHouseAddress.this, null, SPContants.USER_ID, "") + "";
                presenter.queryMyEmeter(uid,"","");
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                // 默认实现，根据实际情况做改动
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        }

//                new PtrDefaultHandler2() {
//            @Override
//            public void onLoadMoreBegin(PtrFrameLayout frame) {
//
//            }
//
//            @Override
//            public void onRefreshBegin(PtrFrameLayout frame) {
//                uid = SPUtils.get(ActivityHouseAddress.this, null, SPContants.USER_ID, "") + "";
//                presenter.queryMyEmeter(uid,"","");
//            }
//        }
        );

        addressListAdapter=new AddressListAdapter(this,meters);
        electricAddressList.setAdapter(addressListAdapter);
        electricAddressList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(ActivityHouseAddress.this, ActivitySmartMeter.class);
                intent.putExtra("xiaoqu_id",meterBeans.get(position).getId());
                intent.putExtra("meter_detail",meters.get(position));
                intent.putExtra("position",position);
                startActivity(intent);

            }
        });
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){

            case R.id.close_icon://返回
                this.finish();
                break;


        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity!=null){
            if (entity.getEventType() == ElectricMeterPresenter.BY_Emeter_address_success){ //我的列表
                commend_anchor_ptr.refreshComplete();
                if (meters.size()>0){
                    meters.clear();
                }
                   meterBeans= (List<ElectricMeterBean>) entity.getData();
                   if (meterBeans.size()>0){
                      for (int i=0;i<meterBeans.size();i++){
                          for (int j=0;j<meterBeans.get(i).getEmeter_list().size();j++){
                              meterBeans.get(i).getEmeter_list().get(j).setAddress(meterBeans.get(i).getAddress()+"_"+meterBeans.get(i).getEmeter_list().get(j).getName());
                              meters.add(meterBeans.get(i).getEmeter_list().get(j));
                          }
                      }
                   }
                   addressListAdapter.addData(meters);

            }else  if (entity.getEventType() == ElectricMeterPresenter.BY_Emeter_address_fail){
                commend_anchor_ptr.refreshComplete();
                ByAlert.alert(entity.getMsg());
            }
        }

    }
}
